package com.acme.bankapp.domain.bank;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.acme.ClientRegistrationListener;
import com.acme.bankapp.domain.exceptions.ClientExistsException;


public class Bank {
	
	private class DebugListener implements ClientRegistrationListener{
			public void onClientAdded(Client c){
				String timeStamp = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(Calendar.getInstance().getTime());
				System.out.println(c + ". Added at: " + timeStamp);
			}

	}
	
	private static Bank instance;
	private List<Client> clients; // List of our clients. Not array, because in list we can dynamically add objects (in arrays too, but List is better for these objectives)
	private List<ClientRegistrationListener> listeners;
	
	public Bank(){
		this.clients = new ArrayList<Client>(); // Init out List of Client
		listeners = new ArrayList<ClientRegistrationListener>();
		DebugListener dl = new DebugListener();
		listeners.add(dl);
	}
	
	public Bank(ClientRegistrationListener[] addedListeners){
		this.clients = new ArrayList<Client>(); // Init out List of Client
		listeners = new ArrayList<ClientRegistrationListener>();
		DebugListener dl = new DebugListener();
		listeners.add(dl);
		for (int i = 0; i < addedListeners.length; i++){
			listeners.add(addedListeners[i]);
		}
	}
	
	public void addClientAddedListener(ClientRegistrationListener listener) {
		this.listeners.add(listener);
	}
	
	public void addClient(Client cl) throws ClientExistsException{
		for (int i = 0; i < this.clients.size(); i++){
			if (this.clients.get(i).equals(cl)){
				throw new ClientExistsException();
			}
		}
		this.clients.add(cl); // Add new Client to Bank
		for( int i = 0; i < listeners.size(); i++ ) {
			listeners.get(i).onClientAdded(cl);
		}
	}
	
	public List<Client> getClients(){
		return this.clients; // Get list of Bank's clients
	}
	
	public int getClientsCount(){
		return clients.size(); // Get length of list (I have know idea how can I use it now :)). "just because I can"
	}
}
